﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Camalot.Common {
	public class DateTimeZone {

		public DateTimeZone ( DateTime dateTime, TimeZoneInfo timezone ) {
			TimeZone = timezone;

			if ( dateTime.Kind == DateTimeKind.Local ) {
				Utc = TimeZoneInfo.ConvertTimeToUtc ( dateTime, TimeZoneInfo.Local );
			} else {
				Utc = dateTime;
			}
		}

		public DateTimeZone ( DateTime dateTime ) {
			TimeZone = TimeZoneInfo.FindSystemTimeZoneById ( "Eastern Standard Time" );
			if ( dateTime.Kind == DateTimeKind.Local ) {
				Utc = TimeZoneInfo.ConvertTimeToUtc ( dateTime, TimeZoneInfo.Local );
			} else {
				Utc = dateTime;
			}
		}

		public DateTimeZone ( )
			: this ( DateTime.UtcNow ) {

		}

		//[JsonConverter ( typeof ( UnixDateTimeConverter ) )]
		//[JsonConverter ( typeof ( IsoDateTimeConverter ) )]
		public DateTime Utc { get; private set; }
		//[JsonIgnore]
		public TimeZoneInfo TimeZone { get; private set; }
		//[JsonProperty ( "TimeZone" )]
		private InternalTimeZoneInfo JsonTimeZoneInfo {
			get {
				return new InternalTimeZoneInfo ( this.TimeZone );
			}
		}
		//[JsonConverter ( typeof ( UnixDateTimeConverter ) )]
		//[JsonConverter ( typeof ( IsoDateTimeConverter ) )]
		public DateTime Local {
			get {
				return TimeZoneInfo.ConvertTime ( Utc, TimeZone );
			}
		}

		private class InternalTimeZoneInfo {
			public InternalTimeZoneInfo ( ) {

			}
			public InternalTimeZoneInfo ( TimeZoneInfo tzi ) {
				this.Id = tzi.Id;
				this.BaseUtcOffset = tzi.BaseUtcOffset;
			}
			public TimeSpan BaseUtcOffset { get; set; }
			public String Id { get; set; }
			public TimeZoneInfo ToTimeZoneInfo ( ) {
				return TimeZoneInfo.FindSystemTimeZoneById ( Id );
			}
		}

	}
}
